import { CashbackTypes } from "@/api/cashback";
import { UserInfoRep, UserVipInfo } from "@/api/user";
import { getMoneyApi } from "@/api/userWallt";
import { server } from "@/utils/server";
import ItemCom from "./component/ItemCom";
import ModalCom from "./component/ModalCom";
import "./page.scss";
import { ProfileHeader } from "./ProfileHeader";

const getUserInfo = async () => {
    return server
        .request<UserInfoRep>({
            url: "/v1/api/user/user_info",
            method: "POST",
            next: { revalidate: 0 },
        })
        .then((res) => {
            if (res.code === 200) return res.data;
            return {};
        });
};

/**
 * @description  前台用户VIP信息 接口地址：https://app.apifox.com/link/project/4790544/apis/api-201160713
 */
const getVipApi = async () => {
    return server
        .request<UserVipInfo>({
            url: "/v1/api/user/user_vip_info",
            method: "POST",
            cache: "no-cache",
        })
        .then((res) => {
            if (res.code === 200) return res.data;
        });
};

const getCashBackApi = async () => {
    return server
        .request<CashbackTypes>({
            url: "/v1/api/front/activity_cash",
            method: "post",
        })
        .then((res) => {
            return res.data;
        })
        .catch((error) => {
            return {
                rules: [],
                last_period: { end_time: 0, start_time: 0 },
                next_period: {
                    end_time: 0,
                    start_time: 0,
                },
                amount: 0,
                bet: 0,
                status: "expired",
            };
        });
};
export const dynamic = "force-dynamic";
export const revalidate = 0;
const Profile = async () => {
    const userInfo = await getUserInfo();
    const userMoney = await getMoneyApi();
    const userVip = await getVipApi();
    const cashback = await getCashBackApi();
    const max = cashback.rules.reduce(
        (acc, item) => (acc > item.cashback ? acc : item.cashback),
        0
    );

    return (
        <div className="profile-box">
            <ProfileHeader userInfo={userInfo} userMoney={userMoney} userVip={userVip!} />
            <ItemCom max={max} />
            <ModalCom />
        </div>
    );
};

export default Profile;
